package initial

import (
	"fmt"

	"fiber-demo/config"
	"fiber-demo/internal/global"

	"github.com/spf13/viper"
)

func InitConfig() {
	viper.SetConfigName("config") // name of config file (without extension)
	viper.SetConfigType("toml")   // REQUIRED if the config file does not have the extension in the name
	viper.AddConfigPath("config") // path to look for the config file in
	err := viper.ReadInConfig()   // Find and read the config file
	if err != nil {               // Handle errors reading the config file
		panic(fmt.Errorf("fatal error config file: %w", err))
	}
	var conf config.Config
	err = viper.Unmarshal(&conf) // 解析配置文件到结构体
	if err != nil {
		fmt.Printf("Failed to unmarshal config: %s", err)
		return
	}
	global.Config = conf
}
